import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Transaction {
    /*交易类
    */
    public static void main(String[] args) {
        mainMenu();
    }

    static String date;//日期
    static double amount;//金额
    static String category;//类别
    static String note;//备注
    private static ArrayList<String[]> transactions = new ArrayList<>(); // 动态数组，存储每一笔收入和支出记录

    public Transaction(String date, double amount, String category, String note) {
        this.date = date;
        this.amount = amount;
        this.category = category;
        this.note = note;
        addTransaction(date, amount, category, note);
    }

    public void addTransaction(String date, double amount, String category, String note) {
        String[] record = new String[]{date, String.valueOf(amount), category, note};
        transactions.add(record);
    }

    public static void mainMenu() {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        while (flag) {
            System.out.println("==============================");
            System.out.println("欢迎使用个人账单管理系统");
            System.out.println("==============================");
            System.out.println("请选择操作：\n" +
                    "1. 记录收入\n" +
                    "2. 记录支出\n" +
                    "3. 查看所有账单\n" +
                    "4. 查询账单\n" +
                    "5. 退出系统\n" +
                    "\n" +
                    "请输入选项序号：");
            int options = sc.nextInt();
            sc.nextLine(); // 消耗换行符,如果不添加就出现‘还没输入日期就弹出日期格式无效’
            switch (options) {
                case 1:
                    record_income(sc);
                    break;
                case 2:
                    record_expenditure(sc);
                    break;
                case 3:
                    display(sc);
                    break;
                case 4:
                    view(sc);
                    break;
                case 5:
                    flag = false;
                    System.out.println("退出菜单");
                    break;
                default:
                    System.out.println("无效选项，请重新输入。");
                    break;
            }
        }
        sc.close();
    }

    public static void record_income(Scanner sc) {
        //记录收入:用户可以录入新的收入情况，每条记录包括：日期、金额（收入金额必须为正数）、类别（如工资、奖金等）和备注
        System.out.println("请输入收入信息：");
        String a = getValidDate(sc);
        System.out.println("金额：");
        double b = sc.nextDouble();
        if (b < 0) {// 检查数值是否为负数
            System.out.println("输入无效，请输入一个非负数值。");
            return;
        }
        System.out.println("类别（如工资、奖金等）：");
        String c = sc.next();
        System.out.println("备注：");
        String d = sc.next();
        new Transaction(a, b, c, d);
        System.out.println("收入已成功记录！");
        System.out.println("\n按任意键返回主菜单..");
        sc.nextLine();//按任意键返回主菜单..
    }

    public static void record_expenditure(Scanner sc) {
        //记录支出：用户可以录入新的支出情况，每条记录包括：日期、金额（支出金额必须为正数）、类别（如餐饮、交通、购物等）和备注。
        System.out.println("请输入支出信息:");
        String a = getValidDate(sc);
        System.out.println("金额：");
        double b = sc.nextDouble();
        if (b < 0) {// 检查数值是否为负数
            System.out.println("输入无效，请输入一个非负数值。");
            return;
        }
        System.out.println("类别（如餐饮、交通、购物等）：");
        String c = sc.next();
        System.out.println("备注：");
        String d = sc.next();
        new Transaction(a, -b, c, d);
        System.out.println("支出已成功记录！");
        System.out.println("\n按任意键返回主菜单..");
        sc.nextLine();//按任意键返回主菜单..
    }

    public static void display(Scanner sc) {
        //账单展示：系统能够展示所有录入的账单记录，收入和支出分开显示。
        System.out.println("所有账单记录：");
        System.out.println("日期\t\t金额\t\t类别\t\t备注");
        for (String[] record : transactions) {
            System.out.println(record[0] + "\t" + record[1] + "\t" + record[2] + "\t" + record[3]);
        }
        System.out.println("\n按任意键返回主菜单..");
        sc.nextLine();//按任意键返回主菜单..
    }

    public static void view(Scanner sc) {
        //账单查询：用户可以按照指定日期、日期范围或类别查询账单，收入和支出分开显示。
        System.out.println("请选择查询方式：\n" +
                "1. 按日期查询\n" +
                "2. 按日期范围查询\n" +
                "3. 按类别查询\n" +
                "4. 返回主菜单");
        int option = sc.nextInt();
        sc.nextLine(); // 消耗换行符
        switch (option) {
            case 1:
                System.out.println("请输入查询日期（YYYY-MM-DD）：");
                String date = sc.nextLine();
                queryByDate(date);
                break;
            case 2:
                System.out.println("请输入起始日期（YYYY-MM-DD）：");
                String startDate = sc.nextLine();
                System.out.println("请输入结束日期（YYYY-MM-DD）：");
                String endDate = sc.nextLine();
                queryByDateRange(startDate, endDate);
                break;
            case 3:
                System.out.println("请输入查询类别：");
                String category = sc.nextLine();
                queryByCategory(category);
                break;
            case 4:
                return;
            default:
                System.out.println("无效选项，请重新输入。");
                break;
        }
        System.out.println("\n按任意键返回主菜单..");
        sc.nextLine();//按任意键返回主菜单..
    }

    private static void queryByDate(String date) {
        System.out.println("查询结果：");
        System.out.println("日期\t\t金额\t\t类别\t\t备注");
        for (String[] record : transactions) {
            if (record[0].equals(date)) {
                System.out.println(record[0] + "\t" + record[1] + "\t" + record[2] + "\t" + record[3]);
            }
        }
    }

    private static void queryByDateRange(String startDate, String endDate) {
        System.out.println("查询结果：");
        System.out.println("日期\t\t金额\t\t类别\t\t备注");
        for (String[] record : transactions) {
            if (record[0].compareTo(startDate) >= 0 && record[0].compareTo(endDate) <= 0) {
                System.out.println(record[0] + "\t" + record[1] + "\t" + record[2] + "\t" + record[3]);
            }
        }
    }

    private static void queryByCategory(String category) {
        System.out.println("查询结果：");
        System.out.println("日期\t\t金额\t\t类别\t\t备注");
        for (String[] record : transactions) {
            if (record[2].equals(category)) {
                System.out.println(record[0] + "\t" + record[1] + "\t" + record[2] + "\t" + record[3]);
            }
        }
    }

    private static String getValidDate(Scanner sc) {
        String a = null;
        boolean isValidDate = false;
        while (!isValidDate) {
            System.out.println("日期（YYYY-MM-DD)：");
            a = sc.nextLine();
            if (a.matches("\\d{4}-\\d{2}-\\d{2}")) {
                isValidDate = true;
            } else {
                System.out.println("日期格式无效，请输入正确的日期格式（YYYY-MM-DD）。");
            }
        }
        return a;
    }
}